package com.enbatis.mybatisplugs.controller;

import com.enbatis.mybatisplugs.base.BaseController;
import com.enbatis.mybatisplugs.commons.sql.Wrapper;
import com.enbatis.mybatisplugs.commons.utils.ResultReturn;
import com.enbatis.mybatisplugs.plugin.Pages;
import com.github.pagehelper.Page;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.enbatis.mybatisplugs.entity.TCategory;
import com.enbatis.mybatisplugs.service.TCategoryService;


import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("v1/t_category")
public class TCategoryController extends BaseController {

@Autowired
private TCategoryService tCategoryService;


    @PostMapping("/add")
    public ResultReturn<Object> add(@RequestBody TCategory tCategory){

        if (StringUtils.isBlank(tCategory.getName())){
            return failed("类别名称不能为空");
        }
        tCategory.setCompanyId(getAccount().getCompanyId());
        tCategory.setUid(getAccount().getId());
        tCategory.setCtime(new Date());
        return  success(tCategoryService.insert(tCategory));
    }


    @DeleteMapping("/delete")
    public ResultReturn<Integer> delete(@RequestParam("id") Long id){
        return success(tCategoryService.deleteById(id));
    }


    @PutMapping("/{id}")
    public  ResultReturn<Integer> update(@PathVariable("id") Long id,@RequestBody TCategory tCategory){
        tCategory.setId(id);
        return success(tCategoryService.updateById(tCategory));
    }


    @GetMapping("/{id}")
    public ResultReturn<TCategory> get(@PathVariable("id") Long id) {
        return success(tCategoryService.getById(id));
    }


    @GetMapping("/list")
    public ResultReturn<List<TCategory>> getList(TCategory tCategory){
        return success(tCategoryService.list(new Wrapper<>(tCategory)));
    }


    @GetMapping("/list_category")
    public ResultReturn<List<TCategory>> getListCategory(TCategory tCategory){
        return success(tCategoryService.list(new Wrapper<>(tCategory).setSqlSelect("id","name")));
    }


    @ApiOperation(value = "分页列表", notes = "分页查询数据列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "请求数据的页码", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "rows", value = "每页条数", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "sidx", value = "排序字段", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "sord", value = "排序方式", paramType = "query", dataType = "String"),
    })
    @PostMapping("/page")
    public ResultReturn<Pages<TCategory>> getPage(@RequestBody TCategory category){
        Page<TCategory> page= getPage();
        return success(tCategoryService.page(page,new Wrapper<>(category)));
    }

}
